﻿#!/usr/bin/env python
#coding:utf-8
import re,os,time,sys
import json
import traceback
try:import cPickle as pickle
except:import pickle


#========================================================================
#私有函数
#获取md5 --> get_md5(src)
#清除换行,制表符 --> clear(data)
#正则抓取 --> re_one(rule,body)/re_all(rule,body)
#获取当前时间13位时间戳 --> timex(x=13)
#获得任意时间的时间戳 --> time_change(time_input,format='%Y-%m-%d %H:%M:%S')
#日志文件 --> log(data,filename="log.txt")
#发邮件 --> send_mail(to_list,sub="test",content="test")
#保存读取对象 --> save_pickle(data,filename)/load_pickle(filename)
#现在时间 --> time_now()
#========================================================================

#获取md5
def get_md5(src):
    import hashlib
    m2 = hashlib.md5()   
    m2.update(src)   
    return m2.hexdigest()

#清除换行，制表符
def clear(data):
    return data.replace("\n","").replace("\r","").replace("\t","")

#正则抓取-单个内容
def re_one(rule,body):
    rule_all = re.findall(re.compile(r'%s' %(rule)),body)
    if len(rule_all)>0:
        return rule_all[0]
    else:
        return False
#正则抓取-列表内容
def re_all(rule,body):
    rule_all = re.findall(re.compile(r'%s' %(rule)),body)
    if len(rule_all)>0:
        return rule_all
    else:
        return False 

#获取当前时间13位时间戳
def timex(x=13):
    return ("%d"%(time.time()*1000))[0:x]

#获得任意时间的时间戳
def time_change(time_input,format='%Y-%m-%d %H:%M:%S'):
    try:
        return time.mktime(time.strptime(time_end,format))
    except :
        return False

#现在时间
def time_now():
    return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))

#日志文件
def log(data,filename="log.txt",time_mode=True):
    if time_mode == True:
        time_now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))+" "
    else:
        time_now = ""
    fh = open(filename,"a")
    fh.write(time_now+data+"\n")
    fh.close()

#捕捉错误返回
def log_wrong(state=False):
    f=open("log_error.log",'a')
    f.write("="*50+"\n"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"\n"+"="*50+"\n")
    traceback.print_exc(file=f)  
    f.flush()  
    f.close()
    return state

#保存读取对象
def save_pickle(data,filename):
    f = file(filename, 'w')
    pickler = pickle.Pickler(f)
    pickler.dump(data)
    f.close()
def load_pickle(filename):
    f = file(filename, 'r')
    unpickler = pickle.Unpickler(f)
    c = unpickler.load()
    return c

#发邮件
def send_mail(to_list,sub="test",content="test"):
    import smtplib
    from email.mime.text import MIMEText
    mail_host="smtp.126.com"
    mail_user="telisa998"
    mail_pass="spada008"
    mail_postfix="126.com"
    if type(to_list)==list:
        pass
    else:
        to_list=[to_list]
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content)
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pass)
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        return log_wrong()